/**
 * $Id$
 * @author Skautron
 * @date   07/09/2011 20:00:45
 *
 * Copyright (C) 2011 Scytl Secure Electronic Voting SA
 *
 * All rights reserved.
 *
 */
package org.sk.gsync.ui;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

/**
 *
 */
public class CustomTrayIcon extends TrayIcon {

    private JFrame parent;

    /**
     * @param image
     * @param tooltip
     */
    public CustomTrayIcon(final Image image) {
        super(image, "SK GSync v.0.1 ALPHA");
        init();
    }

    private void init() {
        if (SystemTray.isSupported()) {
            // fondo=new
            // ImageIcon(getClass().getResource("../Imagenes/fondo.jpg"));
            final JPopupMenu popMenu = new JPopupMenu();
            JMenuItem menuItemExit = new JMenuItem("Exit");
            menuItemExit.addActionListener(new ActionListener() {

                /**
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
                 */
                public void actionPerformed(final ActionEvent e) {
                    System.exit(0);
                }

            });
            JMenuItem menuItemOpen = new JMenuItem("Open");
            menuItemOpen.addActionListener(new ActionListener() {

                /**
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
                 */
                public void actionPerformed(final ActionEvent e) {
                    popMenu.setVisible(false);
                    parent.setVisible(true);
                }

            });
            popMenu.add(menuItemOpen);
            popMenu.addSeparator();
            popMenu.add(menuItemExit);
            addMouseListener(new MouseAdapter() {

                @Override
                public void mouseReleased(final MouseEvent e) {
                    popMenu.setVisible(true);
                    popMenu.setLocation(e.getX(), e.getY() - 80);
                }

            });

            try {
                SystemTray.getSystemTray().add(this);
            } catch (AWTException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        } else {
            JOptionPane
                .showMessageDialog(null,
                    "The Operative System does't support minimize on system tray.");
        }

    }

    /**
     * @param principal
     */
    public void setParent(final JFrame parent) {
        this.parent = parent;
    }
    // public void paint(final Graphics g){
    // g.drawImage(fondo.getImage(), 0, 15, 500,400, this);
    // }

}
